//
//  PlanListViewController.m
//  Patrol
//
//  Created by Lee on 13-2-27.
//  Copyright (c) 2013年 FSTI. All rights reserved.
//

#import "PlanListViewController.h"
#import "planListCell.h"
#import "PatrolServiceClient.h"
#import "FWWorkFormViewController.h"
#import "FWAppDelegate.h"
#import "MBProgressHUD.h"

@interface PlanListViewController ()

@end

@implementation PlanListViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

- (void)viewWillAppear:(BOOL)animated{
    FWAppDelegate *appDelegate = (FWAppDelegate *)[UIApplication sharedApplication].delegate;
    NSString *userName = appDelegate.userName;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    PatrolServiceClient * client = [PatrolServiceClient shareInstance];
    if (client.networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWiFi || client.networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
        [client searchPlain:userName workID:_plainListID resultBlock:^(id outJson, NSError *error) {
            if (!outJson && error!=nil) {
                DLog(@"Threre is somethings wrong ");
                NSString *errorCode = [NSString stringWithFormat:@"%@",error];
                ;
                if ([errorCode rangeOfString:@"-999"].location == NSNotFound ) {
                    [UIUtil alertOKWithTitle:@"错误" withMsg:@"网络错误" withDelegate:nil];
                }
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                return ;
            }
            NSArray *plainArray = outJson;
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            self.list = plainArray;
            [self.tableView reloadData];
        }];
    }else{
        [UIUtil alertOKWithTitle:@"错误" withMsg:@"当前没有连接任何网络" withDelegate:self];
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        return;
    }
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PlanListCellIdentifier";
    PlanListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"PlanListCell" owner:self options:nil];
        if ([array count] > 0) {
            cell = [array lastObject];
        }
    }
    NSUInteger row = [indexPath row];
    NSDictionary *rowDictionary = [_list objectAtIndex:row];
    NSString *work_item = [rowDictionary objectForKey:@"work_item"];
    [cell setCellContent:work_item];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FWWorkFormViewController * formVC = [[FWWorkFormViewController alloc] initWithNibName:@"FWWorkFormViewController" bundle:nil];
    NSDictionary * rowDictionary = _list[indexPath.row];
    NSString * _workID = rowDictionary[@"work_id"];
    formVC.workID = _workID;
    [self.navigationController pushViewController:formVC animated:YES];
    return;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    cell.backgroundColor = [UIColor clearColor];
    NSUInteger row = [indexPath row];
    UIView *v = [[UIView alloc] initWithFrame:cell.frame];
    if (row % 2) {
        v.backgroundColor = [UIColor whiteColor];
    }else{
        UIColor *backgroundColor = [UIColor colorWithRed:(251.0/255.0) green:(248.0/255.0) blue:(243.0/255.0) alpha:1.0];
        v.backgroundColor = backgroundColor;
    }
    cell.backgroundView = v;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSUInteger row = [indexPath row];
    NSDictionary *rowDictionary = [_list objectAtIndex:row];
    NSString *work_item = [rowDictionary objectForKey:@"work_item"];
    return [PlanListCell calcLabelHeightForString:work_item] + 22;
}

@end
